const simpleGit = require('simple-git');
const fs = require('fs-extra');
const path = require('path');

const TEMP_DIR = path.join(__dirname, '__website_files__');

// Clone website from git then move files to the dist folder  
async function deploy({ gitRepo, deployDst }) {
    console.info('1. Removing temporary directory');
    await fs.remove(TEMP_DIR);

    console.info('2. Cloning the repository', gitRepo);
    const git = simpleGit();
    await git.clone(gitRepo, TEMP_DIR, ['--depth=1']);

    console.info('3. Removing the .git folder from the cloned repository');
    await fs.remove(path.join(TEMP_DIR, '.git'));

    console.info('4. Emptying the deployment destination directory');
    await fs.emptyDir(deployDst);

    console.info('5. Moving files from the temporary directory');
    await fs.copy(TEMP_DIR, deployDst);
    await fs.remove(TEMP_DIR);

    return 'Deployment completed successfully.';
}

module.exports = deploy